package greet

import (
	"github.com/gin-gonic/gin"

	"github.com/zeromicro/go-zero/tools/goctl/api/gogen/workspace/internal/domain"
	"github.com/zeromicro/go-zero/tools/goctl/api/gogen/workspace/internal/logic"
	"github.com/zeromicro/go-zero/tools/goctl/api/gogen/workspace/internal/svc"
	"github.com/zeromicro/go-zero/tools/goctl/api/gogen/workspace/pkg/httpx"
)

type Aapi struct {
	svcCtx *svc.ServiceContext
	admin  logic.Admin
	user   logic.User
}

func NewAapi(svcCtx *svc.ServiceContext, admin logic.Admin, user logic.User) *Aapi {
	return &Aapi{
		svcCtx: svcCtx,
		admin:  admin,
		user:   user,
	}
}

func (h *Aapi) InitRegister(engine *gin.Engine) {
	g0 := engine.Group("admin/v1")
	g0.GET("/greet/from/:name", h.GreetHandler)
	g0.GET("/greet/get", h.NoResponseHandler)
}

func (h *Aapi) GreetHandler(ctx *gin.Context) {
	var req domain.Request
	if err := ctx.ShouldBind(&req); err != nil {
		httpx.FailWithErr(ctx, err)
		return
	}

	res, err := h.admin.GreetHandler(ctx, &req)
	if err != nil {
		httpx.FailWithErr(ctx, err)
	} else {
		httpx.OkWithData(ctx, res)
	}
}

func (h *Aapi) NoResponseHandler(ctx *gin.Context) {
	var req domain.Request
	if err := ctx.ShouldBind(&req); err != nil {
		httpx.FailWithErr(ctx, err)
		return
	}
	_ = req
}
